package as3.component.sound
{
	
	import com.seaengine.constData.SoundType;
	import com.seaengine.valueobject.SoundVO;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;
	

	/**
	 * 重写Sound
	 * @author 周玉龙
	 * 二〇一一年十月二十日 13:47:53
	 */	
	public class SoundOverride
	{
	
		//--------------------------------------------------------------------------
		//
		//  variable
		//
		//--------------------------------------------------------------------------
		private var urlRequest:URLRequest;
		
		private var callFunction:Function;
		
		private var channel:SoundChannel;
		
		public function SoundOverride()
		{
			init();
		}
		//--------------------------------------------------------------------------
		//
		//  property
		//
		//--------------------------------------------------------------------------
		private var _id:String

		public function set id(value:String):void
		{
			_id = value;
		}
		
		public function get id():String
		{
			return _id;
		}
		
		private var _url:String

		public function get url():String
		{
			return _url;
		}
		
		private var _sound:Sound;

		public function get sound():Sound
		{
			return _sound;
		}

		private var _vo:SoundVO;

		public function get vo():SoundVO
		{
			return _vo;
		}

		public function set vo(value:SoundVO):void
		{
			_vo = value;
		}
		
		private var _isPlay:Boolean = false;

		/**
		 * 是否播放
		 */		
		public function get isPlay():Boolean
		{
			return _isPlay;
		}
		
		//--------------------------------------------------------------------------
		//
		//  public
		//
		//--------------------------------------------------------------------------
		/**
		 * 加载音乐
		 * @param id           编号
		 * @param callFunction 加载完成回调方法
		 */		
		public function load(url:String,callFunction:Function):void
		{
			_url = url;
			this.callFunction = callFunction;
			try
			{
				urlRequest.url = _url;
				sound.load(urlRequest);
				sound.addEventListener(Event.COMPLETE,soundComplete);
				sound.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
			}
			catch(e:Error)
			{
//				trace(url + "  找不到对应的URL，所以声音无法播放");
			}
		}
		
		
		/**
		 * 播放声音
		 */		
		public function play():void
		{
			if(_sound != null)
			{
				_isPlay = true;
				channel = _sound.play();
				if(_vo.type == SoundType.SCENE || _vo.type == SoundType.BATTLE)
				{
					if(channel.hasEventListener(Event.SOUND_COMPLETE) == false)
					{
						channel.addEventListener(Event.SOUND_COMPLETE,soundPlayComplete);
					}
				}
			}
		}
		
		/**
		 * 停止声音
		 */	
		public function stop():void
		{
			if(_sound != null)
			{
				_isPlay = false;
				channel.stop();
			}
		}
		
		public function destroy():void
		{
			channel.removeEventListener(Event.SOUND_COMPLETE,soundPlayComplete);
			vo = null;
			channel = null;
			_sound = null;
			urlRequest = null;
			callFunction = null;
		}
		
		//--------------------------------------------------------------------------
		//
		//  private
		//
		//--------------------------------------------------------------------------
		private function init():void
		{
			urlRequest = new URLRequest();
			_sound = new Sound();
		}
		
		//--------------------------------------------------------------------------
		//
		//  event
		//
		//--------------------------------------------------------------------------
		private function soundComplete(e:Event):void
		{
			if(_sound != null)
			{
				play();
				if(callFunction != null)
				{
					callFunction.call(callFunction,this);
				}
			}
		}
		
		private function ioErrorHandler(e:IOErrorEvent):void
		{
			//trace(_url + "路径出错");
		}
		
		private function soundPlayComplete(e:Event):void
		{
			play();
		}
	}
}